/*
 *
 *   This file is part of jSevenBar. Copyright 2011 Alex Volodko
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package de.xhelp.jsevenbar.demos;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import de.xhelp.jsevenbar.core.JSevenBar;

/**
 * @author Alex Volodko
 * 
 */
public class MainContainer extends JFrame {
	private JPanel demoList;
	private JSevenBar sevenBar;
	public MainContainer() {
		initiateGui();
		addDemos();
		setLocationRelativeTo(null);
		setTitle("JSevenBar Demo "+Math.random());
		setVisible(true);
		sevenBar = new JSevenBar(this);
	}
	
	private void addDemoToPanel(final Class<? extends AbstractDemo> demoClass, String title, int index) {
		GridBagConstraints c;
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = index;
		c.insets = new Insets(5,5, 5, 5);
		c.anchor = GridBagConstraints.PAGE_START;
		c.weightx = 1.0f;
		c.fill = GridBagConstraints.HORIZONTAL;
		JButton btn = new JButton();
		btn.setText(title);
		btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					AbstractDemo demo = demoClass.newInstance();
					sevenBar.getTabs().addTab(demo);
					//demo.makeModular();
					
				} catch (InstantiationException e1) {
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					e1.printStackTrace();
				}
			}
		});
		demoList.add(btn, c);
	}
	private void addDemos() {
		int i=0;
		addDemoToPanel(DemoProgressBar.class, "Progress bar", i++);
		addDemoToPanel(DemoThumbnail.class, "Thumbnail", i++);
	}
	private void initiateGui() {
		GroupLayout thisLayout = new GroupLayout((JComponent) getContentPane());
		getContentPane().setLayout(thisLayout);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		demoList = new JPanel();
//		GridBagLayout demoListLayout = new GridBagLayout();
//		demoListLayout.columnWidths = new int[] { 7, 7, 7, 7 };
//		demoListLayout.rowHeights = new int[] { 7, 7, 7, 7 };
//		demoListLayout.columnWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
//		demoListLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
		demoList.setLayout(new GridBagLayout());
		demoList.setBorder(BorderFactory.createTitledBorder("Available demos"));
		thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(demoList, 0, 238, Short.MAX_VALUE)
				.addContainerGap());
		thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(demoList, 0, 360, Short.MAX_VALUE)
				.addContainerGap());
		pack();
		setSize(400, 300);
	}

	public static void main(String[] args) {
		new MainContainer();
	}
}
